package com.example.Windturbine.Service.Impl;

import com.example.Windturbine.Dao.TransportDOMapper;
import com.example.Windturbine.Dao.TransportInfoDOMapper;
import com.example.Windturbine.DataObject.MaterialDO;
import com.example.Windturbine.DataObject.MaterialInfoDO;
import com.example.Windturbine.DataObject.TransportDO;
import com.example.Windturbine.DataObject.TransportInfoDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import com.example.Windturbine.Service.Model.MaterialModel;
import com.example.Windturbine.Service.Model.TransportModel;
import com.example.Windturbine.Service.TransportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2021/12/16
 */
@Service
public class TransportServiceImpl implements TransportService {
    @Resource
    TransportDOMapper transportDOMapper;

    @Resource
    TransportInfoDOMapper transportInfoDOMapper;

    @Override
    public List<TransportDO> getAll(Integer level) throws BusinessException {
        List<TransportDO> transportDOS=transportDOMapper.getAll(level);
        if(transportDOS==null){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取失败！");
        }
        return transportDOS;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void submitSingle(TransportModel materialModel) throws BusinessException {
        TransportDO transportDO=new TransportDO();
        TransportInfoDO transportInfoDO=new TransportInfoDO();
        transportDO=materialDOConvertFromMaterialModel(materialModel);
        transportInfoDO=materialInfoDOConvertFromMaterialModel(materialModel);
        if(transportDO==null||transportInfoDO==null){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"提交运输信息失败！");
        }
        try{
            transportDOMapper.insertSelective(transportDO);
            transportInfoDO.setTransportId(transportDO.getId());
            transportInfoDOMapper.insertSelective(transportInfoDO);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"信息有重复注册");
        }
    }

    @Override
    public TransportModel getDetailById(Integer id) throws BusinessException {
        TransportInfoDO transportInfoDO=new TransportInfoDO();
        TransportModel transportModel=new TransportModel();
        try{
            transportInfoDO = transportInfoDOMapper.selectByTransportID(id);
            if(transportInfoDO==null){
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"获取失败！");
            }
            transportModel=convertFromMaterialAndMaterInfo(new TransportDO(),transportInfoDO);
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"查询运输信息失败 信息不存在");
        }

        return transportModel;

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, rollbackFor = Exception.class)
    public void deleteById(Integer id) throws BusinessException {
        try{
            Integer n=transportDOMapper.deleteByPrimaryKey(id);
            if(n!=0){
                transportInfoDOMapper.deleteByTransportId(id);
            }else{
                throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"删除信息失败 系统信息不可删除！");
            }
        }catch(DuplicateKeyException ex){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"删除信息失败 出错！");
        }
    }


    private TransportDO materialDOConvertFromMaterialModel(TransportModel materialModel){
        TransportDO materialDO=new TransportDO(null,materialModel.getName(),materialModel.getTool(),materialModel.getType(),materialModel.getUnit(),materialModel.getRegion(),materialModel.getOrigin(),materialModel.getComments(),materialModel.getLevel());
        return materialDO;
    }

    //根据名字获取第一个原材料的所有信息
    @Override
    public TransportModel getDetailByName(String name,Integer level) throws BusinessException{
        List<TransportDO> all = getAll(level);//获取用户的所有材料信息
        try{
            //获取名字相同的材料
            List<TransportDO> transportDOS = new ArrayList<>();
            for(TransportDO m:all){
                if(m.getName().equals(name)) transportDOS.add(m);
            }
            //取第一个匹配到的材料
            TransportDO first=transportDOS.get(0);
            TransportModel res=getDetailById(first.getId());
            if(res==null) throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"材料"+name+"信息缺失!");
            return res;
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"材料"+name+"信息缺失!");
        }

    }




    private TransportInfoDO materialInfoDOConvertFromMaterialModel(TransportModel materialModel){
        TransportInfoDO materialDO=new TransportInfoDO(null,materialModel.get煤()
                ,materialModel.get石油(),materialModel.get天然气(),materialModel.getCo()
                ,materialModel.getCo2(),materialModel.getSo2(),materialModel.getNox()
                ,materialModel.getCh4(),materialModel.getH2s(),materialModel.getHcl(),
                materialModel.getCod(),materialModel.getNh3(),materialModel.getCfc11()
                ,materialModel.getCfc12(),materialModel.getCfc113(),materialModel.getId());
        return materialDO;
    }

    private TransportModel convertFromMaterialAndMaterInfo(TransportDO materialDO,TransportInfoDO materialInfoDO){
        TransportModel materialModel=new TransportModel(materialDO.getId(),materialDO.getName(),materialDO.getTool(),materialDO.getType(),
                materialDO.getUnit(),materialDO.getRegion(),materialDO.getOrigin(),materialDO.getComments(),materialDO.getLevel(),
                materialInfoDO.getId(),materialInfoDO.get煤(),materialInfoDO.get石油(),materialInfoDO.get天然气(),materialInfoDO.getCo(),
                materialInfoDO.getCo2(),materialInfoDO.getSo2(),materialInfoDO.getNox(),materialInfoDO.getCh4(),materialInfoDO.getH2s(),
                materialInfoDO.getHcl(),materialInfoDO.getCod(),materialInfoDO.getNh3(),materialInfoDO.getCfc11(),
                materialInfoDO.getCfc12(),materialInfoDO.getCfc113());
        return  materialModel;
    }
}
